/*    */ package scripting.reactor;
/*    */ 
/*    */ import client.MapleClient;
/*    */ import com.alibaba.druid.pool.DruidPooledConnection;
/*    */ import database.DatabaseConnection;
/*    */ import java.sql.PreparedStatement;
/*    */ import java.sql.ResultSet;
/*    */ import java.sql.SQLException;
/*    */ import java.util.HashMap;
/*    */ import java.util.LinkedList;
/*    */ import java.util.List;
/*    */ import java.util.Map;
/*    */ import javax.script.Invocable;
/*    */ import javax.script.ScriptEngine;
/*    */ import org.apache.logging.log4j.LogManager;
/*    */ import org.apache.logging.log4j.Logger;
/*    */ import scripting.AbstractScriptManager;
/*    */ import server.maps.MapleReactor;
/*    */ import server.maps.ReactorDropEntry;
/*    */ 
/*    */ public class ReactorScriptManager
/*    */   extends AbstractScriptManager
/*    */ {
/* 24 */   private static final Logger log = LogManager.getLogger(ReactorScriptManager.class.getName());
/* 25 */   private static final ReactorScriptManager instance = new ReactorScriptManager();
/* 26 */   private final Map<Integer, List<ReactorDropEntry>> drops = new HashMap<>();
/*    */   
/*    */   public static synchronized ReactorScriptManager getInstance() {
/* 29 */     return instance;
/*    */   }
/*    */   
/*    */   public void act(MapleClient c, MapleReactor reactor) {
/*    */     try {
/* 34 */       Invocable iv = getInvocable("reactor/" + reactor.getReactorId() + ".js", c);
/* 35 */       if (iv == null) {
/* 36 */         if (c.getPlayer().isAdmin()) {
/* 37 */           c.getPlayer().dropMessage(5, "未找到 Reactor 文件中的 " + reactor.getReactorId() + ".js 文件.");
/*    */         }
/* 39 */         log.info("未找到 Reactor 文件中的 " + reactor.getReactorId() + ".js 文件.");
/*    */         return;
/*    */       } 
/* 42 */       ScriptEngine scriptengine = (ScriptEngine)iv;
/* 43 */       ReactorActionManager rm = new ReactorActionManager(c, reactor);
/*    */       
/* 45 */       scriptengine.put("rm", rm);
/* 46 */       iv.invokeFunction("act", new Object[0]);
/* 47 */     } catch (Exception e) {
/* 48 */       log.error("执行Reactor文件出错 ReactorID: " + reactor.getReactorId() + ", ReactorName: " + reactor.getName(), e);
/*    */     } 
/*    */   }
/*    */   
/*    */   public List<ReactorDropEntry> getDrops(int reactorId) {
/* 53 */     List<ReactorDropEntry> ret = this.drops.get(Integer.valueOf(reactorId));
/* 54 */     if (ret != null) {
/* 55 */       return ret;
/*    */     }
/* 57 */     ret = new LinkedList<>();
/* 58 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 59 */         PreparedStatement ps = con.prepareStatement("SELECT * FROM reactordrops WHERE reactorid = ?")) {
/* 60 */       ps.setInt(1, reactorId);
/* 61 */       try (ResultSet rs = ps.executeQuery()) {
/* 62 */         while (rs.next()) {
/* 63 */           ret.add(new ReactorDropEntry(rs.getInt("itemid"), rs.getInt("chance"), rs.getInt("questid")));
/*    */         }
/*    */       }
/*    */     
/* 67 */     } catch (SQLException e) {
/* 68 */       log.error("从SQL中读取箱子的爆率出错.箱子的ID: " + reactorId, e);
/*    */     } 
/* 70 */     this.drops.put(Integer.valueOf(reactorId), ret);
/* 71 */     return ret;
/*    */   }
/*    */   
/*    */   public void clearDrops() {
/* 75 */     this.drops.clear();
/*    */   }
/*    */ }


/* Location:              C:\PGX\Server.jar!\scripting\reactor\ReactorScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */